﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.Revit;

namespace HangerSupport
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class cmdDuctWeight : HYNoTranExternalCommand
	{
		public override void FilterProcess()
		{
            base.FilterViewType.Add((Autodesk.Revit.DB.ViewType)4);
            base.FilterViewType.Add((Autodesk.Revit.DB.ViewType)117);
            base.FilterViewType.Add((Autodesk.Revit.DB.ViewType)3);
			base.FilterProcess();
		}

		public override Result HYExecute()
		{
			Result result;
			try
			{
				SelectLines selectLines = new SelectLines(base.Doc, base.Sel);
				List<Duct> list = selectLines.GetMEPCurves(new DuctFilter()).Cast<Duct>().ToList<Duct>();
				list = (from x in list
                        where (int)x.GetConnectors().First<Connector>().Shape == 1
				select x).ToList<Duct>();
				bool flag = list.Count <= 0;
				if (flag)
				{
					YJKMessageBox.Information("目前只支持矩形风管计算！");
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					List<SystemLine> systemLines = selectLines.GetSystemLines(list.Cast<MEPCurve>().ToList<MEPCurve>());
					DlgDuctWeightCal dlgDuctWeightCal = new DlgDuctWeightCal(systemLines);
					dlgDuctWeightCal.ShowDialog();
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException ex)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex2)
			{
				YJKMessageBox.Information(ex2.Message.ToString());
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}
	}
}
